package controllers

import (
	"WORKSPACE/models"
	"WORKSPACE/pkg/app"
	"WORKSPACE/pkg/e"
	"WORKSPACE/services"
	"github.com/gin-gonic/gin"
	"github.com/unknwon/com"
)

func GetConsumer(c *gin.Context)  {
	err, info, total := services.GetAllconsumer()
	if err !=nil{
		app.Error(c,e.ERROR,err,err.Error())
		return
	}
	app.OK(c, map[string]interface{}{"value": info, "total": total},"查询成功")
}

func DelConsumer(c *gin.Context){
	id := -1
	if arg := c.PostForm("consumerId"); arg != "" {
		id = com.StrTo(arg).MustInt()
	}
	if id == -1{
		app.INFO(c,30001,"参数错误")
		return
	}
	err := services.Delconsumer(id)
	if err !=nil{
		app.Error(c,e.ERROR,err,err.Error())
		return
	}
	app.OK(c, map[string]interface{}{},"删除成功")
}

func UpdateConsumer(c *gin.Context){
	id := c.PostForm("consumerId")
	id2 := com.StrTo(id).MustInt()
	name:=c.PostForm("consumerName")
	address:=c.PostForm("consumerAddress")
	postcode:= c.PostForm("consumerPostcode")
	phone:= c.PostForm("consumerPhone")

	if id == ""{
		app.INFO(c, 30000, "参数非法")
		return
	}
	err := services.Updateconsumer(models.Consumer{Consumer_id: id2, Consumer_name: name, Consumer_address: address, Consumer_postcode: postcode, Consumer_phone: phone})
	if err !=nil{
		app.Error(c,e.ERROR,err,err.Error())
		return
	}
	app.OK(c, map[string]interface{}{},"更新成功！")
}

func AddConsumer(c *gin.Context){
	id := c.PostForm("consumerId")
	id2 := com.StrTo(id).MustInt()
	name:=c.PostForm("consumerName")
	address:=c.PostForm("consumerAddress")
	postcode:= c.PostForm("consumerPostcode")
	phone:= c.PostForm("consumerPhone")

	err := services.Addconsumer(models.Consumer{Consumer_id: id2, Consumer_name: name, Consumer_address: address, Consumer_postcode: postcode, Consumer_phone: phone})
	if err !=nil{
		app.Error(c,e.ERROR,err,err.Error())
		return
	}
	app.OK(c, map[string]interface{}{},"添加成功！")
}